<template>
  <div class="list-table">
    <div class="area-header">
      <span class="area-header-title">{{ $t('旗下基金') }}</span>
    </div>
    <el-form ref="queryForm" :model="queryParams" :inline="true" @submit.prevent.native>
      <el-form-item :label="$t('基金全称')" prop="fundName">
        <el-input
          v-model="queryParams.fundName"
          :placeholder="$t('基金全称')"
          clearable
          :onsubmit="false"
          @keyup.enter.native="queryHandle"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="queryHandle">{{ $t('搜索') }}
        </el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">{{ $t('重置') }}</el-button>
      </el-form-item>
    </el-form>

    <el-table v-loading="loading" tooltip-effect="darkTable" :show-summary="true" :summary-method="getSummaries" :data="gpItFundList" border>
      <el-table-column :label="$t('序号')" fixed="left" type="index" align="center" show-overflow-tooltip width="50">
        <template slot-scope="scope">
          {{ (queryParams.pageNum - 1) * queryParams.pageSize + scope.$index + 1 }}
        </template>
      </el-table-column>
      <el-table-column :label="$t('基金全称')" fixed="left" align="center" prop="fundName" show-overflow-tooltip min-width="200">
        <template slot-scope="scope">
          <router-link
            target="_blank"
            :to="{path:'/fund/fundBasehome',query:{id:scope.row.fundId}}"
            class="link_a"
          >{{ scope.row.fundName }}
          </router-link>
        </template>
      </el-table-column>
      <el-table-column :label="$t('基金状态')" align="center" prop="fundStatus" min-width="130" :formatter="fundStatusFormat" />
      <el-table-column :label="$t('负责人')" align="center" prop="chargePerson" min-width="130" show-overflow-tooltip />
      <el-table-column :label="$t('基金成立日')" align="center" prop="fundSetDate" show-overflow-tooltip min-width="130">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.fundSetDate) }}</span>
        </template>
      </el-table-column>
      <el-table-column :label="$t('总认缴金额') + $t('金额单位')" align="center" prop="totalSubscribedAmount" width="140">
        <template slot-scope="scope">
          <span class="money-text">{{ moneyFormat(scope.row.totalSubscribedAmount, 'w') }}</span>
        </template>
      </el-table-column>
      <el-table-column :label="$t('总实缴金额') + $t('金额单位')" align="center" prop="totalPaidInAmount" width="140">
        <template slot-scope="scope">
          <span class="money-text">{{ moneyFormat(scope.row.totalPaidInAmount, 'w') }}</span>
        </template>
      </el-table-column>
      <el-table-column :label="$t('基金估值') + $t('金额单位')" align="center" prop="fundValuation" width="140">
        <template slot-scope="scope">
          <span class="money-text">{{ moneyFormat(scope.row.fundValuation, 'w') }}</span>
        </template>
      </el-table-column>
      <el-table-column :label="$t('已投资项目金额') + $t('金额单位')" align="center" prop="investDealAmount" width="140">
        <template slot-scope="scope">
          <span class="money-text">{{ moneyFormat(scope.row.investDealAmount, 'w') }}</span>
        </template>
      </el-table-column>
      <el-table-column :label="$t('已分配出资人金额') + $t('金额单位')" align="center" prop="investorFpAmount" width="150">
        <template slot-scope="scope">
          <span class="money-text">{{ moneyFormat(scope.row.investorFpAmount, 'w') }}</span>
        </template>
      </el-table-column>
      <el-table-column :label="$t('Gross IRR')" align="center" prop="fundGrossIrr" width="140">
        <template slot-scope="scope">
          <span>{{ moneyFormat(scope.row.fundGrossIrr) }}</span>
        </template>
      </el-table-column>
      <el-table-column :label="$t('Net IRR')" align="center" prop="fundNetIrr" width="140">
        <template slot-scope="scope">
          <span>{{ moneyFormat(scope.row.fundNetIrr) }}</span>
        </template>
      </el-table-column>
      <el-table-column :label="$t('在管AUM') + $t('金额单位')" align="center" prop="manageAUM" width="140">
        <template slot-scope="scope">
          <span class="money-text">{{ moneyFormat(scope.row.manageAUM, 'w') }}</span>
        </template>
      </el-table-column>
      <el-table-column :label="$t('币种')" align="center" prop="fundCurrency" :formatter="currencyFormat" min-width="130" />
      <!-- <el-table-column :label="$t('操作')" align="center" class-name="small-padding fixed-width" width="120">
        <template slot-scope="scope">
          <el-button
            v-hasPermi="['gp:itFund:view']"
            size="small"
            type="text"
            icon="el-icon-view"
            @click="updateHandle(scope.row)"
          >{{ $t('查看') }}
          </el-button>
        </template>
      </el-table-column>-->
    </el-table>
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
  </div>
</template>

<script>
import { getGpItFundSum, listGpItFund } from '@/api/fund/gp/itFund/gpInternalFund'
export default {
  data() {
    return {
      // 遮罩层
      loading: true,
      // 总条数
      total: 0,
      // 内部基金管理人-财务信息表格数据
      gpItFundList: [],
      // 查询参数
      queryParams: {
        gpId: this.$route.query.gpId,
        fundName: undefined,
        pageNum: 1,
        pageSize: 10
      },
      // 表单参数
      form: {},
      sumForm: {},
      // 表单校验
      rules: {},
      // 币种字典
      currencyOptions: [],
      // 基金状态字典
      fundStatusOptions: []
    }
  },
  created() {
    this.getDicts('currency').then(response => {
      this.currencyOptions = response.data
    })
    this.getDicts('fund_status').then(response => {
      this.fundStatusOptions = response.data
    })
    this.getList()
  },
  methods: {
    /** 查询内部基金管理人-财务信息列表 */
    getList() {
      this.loading = true
      listGpItFund(this.queryParams).then(response => {
        this.gpItFundList = response.rows
        this.total = response.total
        getGpItFundSum({ gpId: this.$route.query.gpId, fundName: this.queryParams.fundName }).then(res => {
          this.sumForm = res.data
        })
        this.loading = false
      })
    },
    // 注册资本币种字典翻译
    currencyFormat(row) {
      return this.selectDictLabel(this.currencyOptions, row.fundCurrency)
    },
    // 基金状态字典翻译
    fundStatusFormat(row) {
      return this.selectDictLabel(this.fundStatusOptions, row.fundStatus)
    },

    // 表单重置
    reset() {
      this.form = {
        gpId: this.$route.query.gpId,
        fundName: undefined
      }
      this.resetForm('form')
    },
    /** 搜索按钮操作 */
    queryHandle() {
      this.queryParams.pageNum = 1
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm('queryForm')
      this.queryHandle()
    },
    getSummaries(param) {
      const { columns } = param
      const sums = []
      columns.forEach((column, index) => {
        if (index === 0) {
          sums[index] = '合计'
        } else if (index === 5) {
          sums[index] = this.moneyFormat(this.sumForm.totalSubscribedAmount, 'w')
        } else if (index === 6) {
          sums[index] = this.moneyFormat(this.sumForm.totalPaidInAmount, 'w')
        } else if (index === 7) {
          sums[index] = this.moneyFormat(this.sumForm.fundValuation, 'w')
        } else if (index === 8) {
          sums[index] = this.moneyFormat(this.sumForm.investDealAmount, 'w')
        } else if (index === 9) {
          sums[index] = this.moneyFormat(this.sumForm.investorFpAmount, 'w')
        } else if (index === 12) {
          sums[index] = this.moneyFormat(this.sumForm.manageAUM, 'w')
        } else {
          sums[index] = ''
        }
      })
      return sums
    }
    // 合计
    // getSummaries(param) {
    //   console.log(param)
    //   const {
    //     columns,
    //     data
    //   } = param
    //   const sums = []
    //   columns.forEach((column, index) => {
    //     if (index < 1) {
    //       sums[index] = '合计'
    //     } else if (index >= 5 && index <= 9 || index === 12) {
    //       const values = data.map(item => Number(item[column.property]))
    //       if (!values.every(value => isNaN(value))) {
    //         sums[index] = values.reduce((prev, curr) => {
    //           const value = Number(curr)
    //           if (!isNaN(value)) {
    //             return prev + curr
    //           } else {
    //             return prev
    //           }
    //         }, 0)
    //          sums[index] = moneyFormat(sums[index], 'w')
    //       } else {
    //         sums[index] = 'N/A'
    //       }
    //     } else {
    //       sums[index] = ''
    //     }
    //   })
    //   return sums
    // }
  }
}
</script>
